﻿<%@ WebHandler Language="C#" Class="RichECMS.IHandler.submitmsg" %>

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Web;
using System.Web.SessionState;
using System.IO;
using System.Text;
using System.Collections;
using System.Net;
using System.Xml;
using BusinessLogic.ClassFactory;
using Common;
using DataAccess;
using LitJson;
using RichECMS;

namespace RichECMS.IHandler
{
    public class submitmsg : IHttpHandler, IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.AddHeader("Content-Type", "application/json; charset=UTF-8");
            Hashtable result = new Hashtable();

            string strmsgtitle = HttpContext.Current.Server.UrlDecode(HttpContext.Current.Request["title"]);
            string strmsgchvSummary = HttpContext.Current.Server.UrlDecode(HttpContext.Current.Request["summary"]);
            string strmsgcontent = HttpContext.Current.Server.UrlDecode(HttpContext.Current.Request["content"]);
            string strScid = HttpContext.Current.Server.UrlDecode(HttpContext.Current.Request["scid"]);
            string strWid = HttpContext.Current.Server.UrlDecode(HttpContext.Current.Request["wid"]);
            int iresult = 0;
            int intSTID = 21;
            int intWID = 21;
            if (!int.TryParse(strScid, out iresult) || !int.TryParse(strWid, out iresult))
            {
                result["success"] = false;
                result["error"] = "参数错误";
                context.Response.Write(
      JsonMapper.ToJson(result));
            }
            else
            {
                intSTID = Convert.ToInt32(strScid);
                intWID = Convert.ToInt32(strWid);

                using (DBManager objDBManager = new DBManager())
                {
                    objDBManager.BeginTransaction();
                    try
                    {
                        ArticleFactory objFactory = new ArticleFactory(objDBManager);
                        BusinessLogic.BusinessRules.Article objArticle = objFactory.NewInstance();
                        //插入文章表
                        objArticle.DataItem.chvKeyWords = ",,,";
                        objArticle.DataItem.chvSummary = strmsgchvSummary;
                        objArticle.DataItem.chvMainPic = "";
                        objArticle.DataItem.chvMainMiniPic = "";
                        objArticle.DataItem.txtContent = strmsgcontent;
                        objArticle.DataItem.dtmInTime = Convert.ToDateTime(DateTime.Now.ToString("yy/MM/dd HH:mm"));
                        objArticle.DataItem.chvTitle = strmsgtitle;
                        objArticle.DataItem.chvSubTitle = "";
                        objArticle.DataItem.chvArticleInMan = "";
                        objArticle.DataItem.chvDescription = "";
                        objArticle.DataItem.dtmIssueTime = Convert.ToDateTime(DateTime.Now.ToString("yy/MM/dd HH:mm"));
                        objArticle.DataItem.intWSID = intWID;

                        objArticle.DataItem.intRecycling = (int)Utility.ArticleState.NoRecycling;

                        objArticle.DataItem.chvVedioPath = "";
                        objArticle.DataItem.chvFilePath = "";
                        objArticle.DataItem.txtSeoTitle = "";
                        objArticle.DataItem.txtSeoKeywords = "";
                        objArticle.DataItem.txtSeodescription = "";
                        objArticle.DataItem.chvArticleSource = "";
                        objArticle.DataItem.chvAuthor = "";

                        objArticle.Insert();
                        //插入关联表
                        ArtSecRelationFactory objAsFactory = new ArtSecRelationFactory(objDBManager);
                        BusinessLogic.BusinessRules.ArtSecRelation objArtSecRelation = objAsFactory.NewInstance();
                        objArtSecRelation.DataItem.intSectionID = intSTID;
                        objArtSecRelation.DataItem.intArticleID = objArticle.DataItem.intArticleID;
                        objArtSecRelation.DataItem.intAccessCount = 0;
                        objArtSecRelation.DataItem.dtmIssueTime = Convert.ToDateTime(DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss", DateTimeFormatInfo.InvariantInfo));
                        objArtSecRelation.DataItem.blnIssue = (int)Utility.ArticleState.NoIssue;
                        objArtSecRelation.DataItem.blnRefers = (int)Utility.ArticleRefersState.NoRefers;

                        objArtSecRelation.DataItem.intSort = 0;
                        objArtSecRelation.DataItem.intState = (int)Utility.ArticleState.NoIssue;
                        objArtSecRelation.DataItem.intType = (int)Utility.ArticleType.Article;
                        objArtSecRelation.DataItem.intAccessCount = 0;
                        objArtSecRelation.DataItem.intVerify = 0;
                        objArtSecRelation.DataItem.intGlobalRec = 0;
                        objArtSecRelation.DataItem.intSectionRec = 0;
                        objArtSecRelation.DataItem.intGlobalTop = 0;
                        objArtSecRelation.DataItem.intSectionTop = 0;
                        objArtSecRelation.DataItem.intScroll = 0;
                        objArtSecRelation.DataItem.intWSID = intWID;

                        objArtSecRelation.Insert();

                        //修改栏目状态
                        SectionsFactory objSFactory = new SectionsFactory(objDBManager);
                        BusinessLogic.BusinessRules.Sections objSections = objSFactory.Creator(intSTID);
                        objSections.DataItem.intIssueState = (int)Utility.STIssueState.NoIssue;

                        objSections.Update();
                        //提交
                        objDBManager.Commit();

                        result["success"] = true;
                        //temp["atitle"] =objArticle.DataItem.chvTitle;
                        objSFactory.Dispose();
                        objAsFactory.Dispose();
                        objFactory.Dispose();
                        context.Response.Write(
              JsonMapper.ToJson(result));
                    }
                    catch (Exception ex)
                    {
                        //Alert(ex.ToString().Replace("\r\n", " "));
                        objDBManager.Rollback();

                        result["success"] = false;
                        result["error"] = ex.Message;
                        //temp["atitle"] =objArticle.DataItem.chvTitle;
                        context.Response.Write(
              JsonMapper.ToJson(result));
                    }
                }
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

    }
}